﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using taigu.WeChat;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.Extensions.Configuration;
using taigu.WebChat;
using Microsoft.AspNetCore.Authorization;

namespace taigu
{
    public partial class AdminService : ApplicationService, IAdminService
    {

        /// <summary>
        /// 后台帖子管理类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, Route("/admin/community/GetUserList")]
        public ResData<object> GetUserList(UserInfoAdminDto dto)
        {
            try
            {
                var list = (
                            from wechatUser in _weChatUsersRepository
                            select new UserInfoSelectDto()
                            {
                                avatarUrl = wechatUser.avatar_url,
                                buId = wechatUser.bu_id,
                                buName = _organizationStructRepository.Where(x => x.Id.ToString() == wechatUser.bu_id).FirstOrDefault().name,
                                companyId = wechatUser.company_id,
                                companyName = _organizationStructRepository.Where(x => x.Id.ToString() == wechatUser.company_id).FirstOrDefault().name,
                                nickName = wechatUser.nick_name,
                                realName = wechatUser.real_name,
                                employee_id=wechatUser.employee_id,
                                email=wechatUser.email,
                                user_type=wechatUser.user_type,
                                status = wechatUser.status,
                                user_id = wechatUser.Id,


                            }
                            );

                if (dto.status == WeChat.UserStatus.disabled)
                {
                    list = list.Where(x => x.status == WeChat.UserStatus.disabled);
                }
                if (dto.status == WeChat.UserStatus.enabled)
                {
                    list = list.Where(x => x.status == WeChat.UserStatus.enabled);
                }

                int count = list.Count();

                if (dto.page < 0)
                    dto.page = 1;
                if (dto.pageSize < 0)
                    dto.pageSize = 10;
                list = list.Skip((dto.page - 1) * dto.pageSize).Take(dto.pageSize);


                return new ResData<object>() { Status = ResStatus.成功, Msg = "sucess", Info = new { count = count, list = list.ToList() } };
            }
            catch (Exception ex)
            {
                return new ResData<object>() { Status = ResStatus.查询失败, Msg = ex.Message, Info = null };
            }
        }

        [Route("/admin/community/UserStatus"), HttpPost]
        public async Task<ResData<string>> UserStatus(UserDto m)
        {
            try
            {

                WeChatUsers model = await _weChatUsersRepository.FirstOrDefaultAsync(x => x.Id == m.user_id);
                if (model == null)
                {
                    return new ResData<string>() { Status = ResStatus.failed, Msg = "sucess", Info = "" };
                }
                else
                {
                    if (model.status == WeChat.UserStatus.disabled)
                        model.status = WeChat.UserStatus.enabled;
                    else
                        model.status = WeChat.UserStatus.disabled;
                    await _weChatUsersRepository.UpdateAsync(model);
                    return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
                }
            }
            catch (Exception ex)
            {
                return new ResData<string>() { Status = ResStatus.failed, Msg = ex.Message, Info = "" };
            }
        }

    }
}
